package com.atguigu.test.juctest.cas;

import java.util.concurrent.TimeUnit;

/**
 * 没有cas(比较并交换之前)
 */
class Student{
   volatile  int number =0;


    public int getNumber() {
        return number;
    }
    //写(需要加锁)
    public synchronized void setNumber() {
        number ++;
    }
}
public class CasBefore {
    public static void main(String[] args) {
        //线程操作资源类
        Student student = new Student();
        for (int i = 1; i <= 200; i++) {
            new Thread(() -> {
                for (int i1 = 1; i1 <= 200; i1++) {
                    student.setNumber();
                }
            }, String.valueOf(i)).start();


        }
         try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); }
         System.out.println(Thread.currentThread().getName()+"\t"+"---come in"+student.getNumber());
    }
}
